<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="案例名称" prop="exampleName">
        <el-input
          v-model="queryParams.exampleName"
          placeholder="请输入案例名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:CompassCaseStudy:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:CompassCaseStudy:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:CompassCaseStudy:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:CompassCaseStudy:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" @cell-dblclick="cellDbClick" :data="CompassCaseStudyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="标题" align="center" prop="exampleName"/>
      <el-table-column label="封面" align="center" prop="listImage">
        <template #default="{row}">
          <img :src="row.listImage" alt="封面" style="width: 120px">
        </template>
      </el-table-column>
      <el-table-column label="案例类型" align="center" prop="exampleType"/>
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="首页精选" align="center">
        <template #default="{row}">
          <span>{{row.indexChosen ? '是' : '否'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="列表精选" align="center" prop="isChosen">
        <template #default="{row}">
          <span>{{row.listChosen ? '是' : '否'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort">
        <template #default="{row}">
          <el-input type="number" v-model="row.sort" @blur="saveSort(row)" autocomplete="on" ref="input"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="点击数量" align="center" prop="lookNum"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:CompassCaseStudy:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:CompassCaseStudy:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateIsChoosen(scope.row, 'index')"
            v-hasPermi="['system:CompassVideoEva:edit']"
          >{{!scope.row.indexChosen ? '设置首页精选' : '取消首页精选'}}
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateIsChoosen(scope.row, 'list')"
            v-hasPermi="['system:CompassVideoEva:edit']"
          >{{!scope.row.listChosen ? '设置列表精选' : '取消列表精选'}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改案例管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body @close="$refs.editor.clearEditor()">
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="案例名称" prop="exampleName">
              <el-input v-model="form.exampleName" placeholder="请输入案例名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="案例类型" prop="exampleType">
              <el-select v-model="form.exampleType" placeholder="请选择案例类型">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="首页封面" prop="indexImage">
              <!--            <el-input v-model="form.indexImage" placeholder="请输入首页图片"/>-->
              <Upload v-model="form.indexImage" @clear="clearImageValidate" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="列表封面" prop="listImage">
              <!--            <el-input v-model="form.listImage" placeholder="请输入列表图片"/>-->
              <Upload v-model="form.listImage" @clear="clearImageValidate" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="点击数量" prop="lookNum">
          <el-input v-model="form.lookNum" placeholder="请输入观看数量"/>
        </el-form-item>
        <el-form-item label="案例内容" prop="actDesc">
          <Editor v-model="form.exampleDesc" ref="editor"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompassCaseStudy,
  getCompassCaseStudy,
  delCompassCaseStudy,
  addCompassCaseStudy,
  updateCompassCaseStudy,
  exportCompassCaseStudy,
  updateIndexChosenFlag,
  updateListChosenFlag
} from '@/api/system/CompassCaseStudy'
import Editor from "@/components/Editor"
import Upload from "@/components/Upload"

export default {
  name: 'CompassCaseStudy',
  components: {
    Editor,
    Upload
  },

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 案例管理表格数据
      CompassCaseStudyList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        exampleName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        exampleName: [
          { required: true, message: '案例名称不能为空', trigger: 'blur' }
        ]
      },

      options: [
        {
          value: '指北星优秀学生',
          label: '指北星优秀学生'
        },
        {
          value: '合作院校',
          label: '合作院校'
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleUpdateIsChoosen(row, type) {
      if (type === 'index') {
        updateIndexChosenFlag({
          id: row.id,
          flag: row.indexChosen ? 0 : 1
        }).then(res => {
          row.indexChosen = !row.indexChosen
          this.msgSuccess('操作成功')
        })
      } else {
        updateListChosenFlag({
          id: row.id,
          flag: row.listChosen ? 0 : 1
        }).then(res => {
          row.listChosen = !row.listChosen
          this.msgSuccess('操作成功')
        })
      }
    },

    saveSort(row) {
      row.edit = false
      const postData = {
        ...row,
        exampleDesc: this.filterContent(row.exampleDesc)
      }
      updateCompassCaseStudy(postData).then(response => {
        if (response.code === 200) {
          this.msgSuccess('修改成功')
        }
      })
    },

    cellDbClick(row, column) {
      if (column.property === 'sort') {
        this.$set(row, 'edit', true)
        this.$nextTick(() => {
          this.$refs.input.focus()
        })
      }
    },

    clearImageValidate() {
      this.$refs["form"].clearValidate("indexImage");
      this.$refs["form"].clearValidate("listImage");
    },

    /** 查询案例管理列表 */
    getList() {
      this.loading = true
      listCompassCaseStudy(this.queryParams).then(response => {
        this.CompassCaseStudyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        listImage: null,
        exampleName: null,
        indexChosen: null,
        indexImage: null,
        sort: null,
        exampleDesc: null,
        exampleType: null,
        lookNum: null,
        listChosen: null,
        updateTime: null,
        createTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加案例'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCompassCaseStudy(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改案例'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const postData = {
            ...this.form,
            exampleDesc: this.filterContent(this.form.exampleDesc)
          }
          if (this.form.id != null) {
            updateCompassCaseStudy(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addCompassCaseStudy(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除案例管理编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCompassCaseStudy(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有案例管理数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportCompassCaseStudy(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>
